<template>
  <div class="statistics-container">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>问卷总数</span>
            </div>
          </template>
          <div class="card-body">
            <h2>{{ statistics.totalQuestionnaires }}</h2>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>用户总数</span>
            </div>
          </template>
          <div class="card-body">
            <h2>{{ statistics.totalUsers }}</h2>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>今日提交</span>
            </div>
          </template>
          <div class="card-body">
            <h2>{{ statistics.todaySubmissions }}</h2>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>总提交数</span>
            </div>
          </template>
          <div class="card-body">
            <h2>{{ statistics.totalSubmissions }}</h2>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <el-col :span="24">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>最近7天统计</span>
            </div>
          </template>
          <div class="chart-container" ref="chartRef"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getStatistics } from '@/api/questionnaire'
import * as echarts from 'echarts'

const statistics = ref({
  totalQuestionnaires: 0,
  totalUsers: 0,
  todaySubmissions: 0,
  totalSubmissions: 0,
  dailyStats: []
})

const chartRef = ref(null)
let chart = null

const initChart = () => {
  if (chart) {
    chart.dispose()
  }
  chart = echarts.init(chartRef.value)
}

const updateChart = () => {
  const dates = statistics.value.dailyStats.map(item => item.date)
  const submissions = statistics.value.dailyStats.map(item => item.submissions)
  const views = statistics.value.dailyStats.map(item => item.views)

  const option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['提交数', '浏览量']
    },
    xAxis: {
      type: 'category',
      data: dates
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '提交数',
        type: 'line',
        data: submissions,
        smooth: true
      },
      {
        name: '浏览量',
        type: 'line',
        data: views,
        smooth: true
      }
    ]
  }

  chart.setOption(option)
}

const fetchData = async () => {
  try {
    const { data } = await getStatistics()
    statistics.value = data
    updateChart()
  } catch (error) {
    console.error('获取统计数据失败:', error)
  }
}

onMounted(() => {
  initChart()
  fetchData()
  window.addEventListener('resize', () => {
    chart?.resize()
  })
})
</script>

<style scoped>
.statistics-container {
  padding: 20px;
}
.box-card {
  margin-bottom: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.card-body {
  text-align: center;
  padding: 20px 0;
}
.card-body h2 {
  margin: 0;
  font-size: 24px;
  color: #409EFF;
}
.chart-row {
  margin-top: 20px;
}
.chart-container {
  height: 400px;
}
</style> 